let api = require('./api.js');
class EasyHttp {
  post(url, data = {}) {
    data.ali_openid = my.getStorageSync({
      key: 'openId'
    }).data
    data.ali_appid = my.getAppIdSync().appId
    data.ali_province_type = 1
    data.client_type = 'zfb'
    return new Promise((resolve, reject) => {
      my.request({
        url,
        data: data,
        method: 'POST',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
        success(res) {
          if (res.data.status == -100 || res.data.status == -101) {
            EasyHttp.loginByWeixin().then(prop => {
              data.ali_openid = my.getStorageSync({
                key: 'openId'
              }).data
              if (prop.data.status == 1) {
                my.request({
                  url,
                  data: data,
                  method: 'POST',
                  header: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                  },
                  success(result) {
                    resolve(result)
                  },
                  fail(err) {
                    reject(reject)
                  }
                })
              }
            }).catch(res => {
              resolve(res)
            })
          } else {
            resolve(res)
          }
        },
        fail(err) {
          reject(reject)
        }
      })
    })
  }
  get(url, data = {}) {
    return new Promise((resolve, reject) => {
      my.request({
        url,
        data: data,
        method: 'GET',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
        success(res) {
          resolve(res)
        },
        fail(err) {
          reject(reject)
        }
      })
    })
  }
  static loginByWeixin() {
    return new Promise(function (resolve, reject) {
      my.getAuthCode({
        scopes: 'auth_base',
        success: function (res) {
          if (res.code) {
            var appid = my.getAppIdSync();
            my.request({
              url: api.reqUrls.miniLogin,
              data: {
                auth_code: res.authCode,
                appid: appid.appId
              },
              method: 'POST',
              header: {
                'Content-Type': 'application/x-www-form-urlencoded',
              },
              success(res) {
                if (res.data.statustype == 200) {
                  my.setStorageSync({
                    key: 'openId',
                    data: res.data.data.openid
                  });
                  if (res.data.data.userid) {
                    my.setStorageSync({
                      key: 'userId',
                      data: res.data.data.userid
                    });
                  }
                  resolve(res)
                }

              },
              fail(err) {
                reject(reject)
              }
            })
          }
        },
        fail: function (err) {
          console.log(err)
        }
      });
    });

  }
}
module.exports = {
  EasyHttp: new EasyHttp
}